# **********************OBJ INFO**************************
# Author:YuKalix
# @Time    : 2019-9-21 22:39
# @Site    : 52ziyu.cn
# @File    : TextRecognitionSpeechOutput.py
# @software: PyCharm
# *********************************************************
from aip import AipSpeech
import pygame
import os

class TextRecognitionSpeechOutput:
    def __init__(self):
        self.APP_ID = '17304319'
        self.API_KEY = 'GZmMep3vMtN8a5hcdeb5KZzT'
        self.SECRET_KEY = 'vg06OhL9zc7tBjAwzOquX0Rdn51W1Ivl'
        self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)

        self.EnvironmentPath = (os.path.abspath('.'))
    def py_game_player(self, file):
        pygame.mixer.init()
        print("播放开始")
        pygame.mixer.music.load(file)
        pygame.mixer.music.play(loops=1, start=0.0)
        print('播放结束')
        while True:
            if pygame.mixer.music.get_busy() == 0:
                print("播报完毕")
                break

    def main(self, result):
        lan = result
        result = self.client.synthesis(lan, 'zh', 1, {'vol': 5, 'per': 1, 'spd': 3})
        '''
        固定值zh。语言选择,目前只有中英文混合模式，填写固定值zh
        客户端类型选择，web端填写固定值1
        spd语速，取值0-15，默认为5中语速(选填)
        pit音调，取值0-15，默认为5中语调（选填）
        vol音量，取值0-15，默认为5中音量（选填）
        per发音人选择, 0为普通女声，1为普通男生，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女声
        '''
        if not isinstance(result, dict):
            with open(r'{0}/speech.mp3'.format(self.EnvironmentPath), 'wb') as f:
                f.write(result)
        self.py_game_player(r'{0}/speech.mp3'.format(self.EnvironmentPath))


if __name__ == '__main__':
    S = TextRecognitionSpeechOutput()
    S.main("hello everone")
